const MessageBoardModelSer = require('../../service/messageBoardModel/MessageBoardModelSer')

class MessageBoardCon {
    // 发表留言
    async addMessage(ctx) {
        const reqData = ctx.request.body
        try {
            const addData = {
                name: reqData.name,
                number: reqData.number,
                message: reqData.message,
                reply_message_id: reqData.replyMessageId || 0,
                img_url: reqData.imgUrl,
                reply_time: 0,
            }
            const data = await MessageBoardModelSer.addMessage(addData)
            if (addData.reply_message_id) {
                await MessageBoardModelSer.editMessage(addData.reply_message_id)
            }
            if (data.dataValues.id) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: "发表失败"
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    //查询留言
    async getMessage(ctx) {
        try {
            const listQuery = ctx.request.body.listQuery || {page: 1, limit: 10}
            const searchData = {}
            if (ctx.request.body.searchData) {
                ctx.request.body.searchData.name && (searchData.name = ctx.request.body.searchData.name)
                ctx.request.body.searchData.number && (searchData.number = ctx.request.body.searchData.number)
                ctx.request.body.searchData.replyMessageId && (searchData.reply_message_id = ctx.request.body.searchData.replyMessageId)
            }
            const data = await MessageBoardModelSer.getMessageTow(listQuery, searchData)
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    //删除留言
    async deleteMessage(ctx) {
        const id = ctx.request.body.id
        try {
            const data = await MessageBoardModelSer.deleteMessage(id)
            ctx.body = {
                code: '200',
                data: data
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


}

module.exports = new MessageBoardCon()
